package top.sdaily.message.push;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jiguang.common.resp.DefaultResult;
import cn.jpush.api.JPushClient;
import cn.jpush.api.device.OnlineStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * Created by soya on 2017/3/12.
 */
@Service
public class JPushManager implements PushManager {


    @Value("${message.push.appKey}")
    private String appKey;
    @Value("${message.push.masterSecret}")
    private String masterSecret;

    private static JPushClient jpushClient;

    @PostConstruct
    public void run(){
        jpushClient = new JPushClient(appKey, masterSecret);
    }

    @Override
    public boolean bindUser(String phoneNumber,String registrationId) throws APIConnectionException, APIRequestException {
        DefaultResult result =  jpushClient.bindMobile(registrationId, phoneNumber);
        System.out.println(result.toString());
        if (result.getResponseCode() == 200) {
            return true;
        }
        return false;
    }

    @Override
    public JPushClient getClient() {
        return jpushClient;
    }
}
